import fs from 'fs'
import path from 'path'
import readline from 'readline'
import { fileURLToPath } from 'url'

const __filename = path.dirname(import.meta.url)
const __dirname = fileURLToPath(__filename)

const fileName = path.resolve(__dirname, '../../logs/access.log')

// 创建 readStream
const readStream = fs.createReadStream(fileName)

// 创建 readline 对象
const rl = readline.createInterface({
	input: readStream
})

let chromeNum = 0
let sum = 0

// 逐行读取
rl.on('line', (lineData) => {
	if (!lineData) {
		return
	}
	sum++

	const arr = lineData.split(' -- ')
	if (arr[2] && arr[2].indexOf('Chrome') > 0) {
		chromeNum++
	}
})

rl.on('close', () => {
	console.log('chrome 占比: ', chromeNum / sum)
})
